﻿@{
    ViewBag.Title = "UCFormsRelationSelectDialog";
    Layout = "~/Views/Shared/_FormLayout.cshtml";
}
@section HeadIncluder 
{
   <link href='@Url.Content("~/Content/public.css")' rel="stylesheet" />
    <link href='@Url.Content("~/Content/con_style.css")' rel="stylesheet" />
}
 
<input id="hProcessGuid" type="hidden"  value="@Request["ProcessGuid"]"/>
<input id="hAppInstanceID" type="hidden"  value="@Request["AppInstanceID"]"/>
<input id="hFormTable" type="hidden"  value="@Request["FormTable"]"/>
<input id="hActivityGuid" type="hidden"  value="@Request["ActivityGuid"]"/>
<table style="height: 400px; width: 780px;margin-left:4px;margin-right:4px;"  width="100%" class="left_xx">
    <tr  style="height:36px;">
        <td style="width: 100%;">流程类型：<select id="slType" class="in_c1" style="height:22px">
            <option value="0"></option>
            @{
                IList<FCDB.Entity.WfProcessType> TypeList = ViewBag.WorkFlowTypeList as IList<FCDB.Entity.WfProcessType>;
                foreach (var item in TypeList)
                {
                <option value="@item.ID">@item.Name</option>
                }
            }

        </select>关键字：<input id="txtkw" type="text" style="height:22px" class="in_c1" />
            <div class="btn_s3 btn_c1">
                <button type="button"  id="btnQuery" onclick="onQuery()" class="fj btn_c3 btn_s">查找</button> 
                </div>
        </td>
    </tr>
    <tr   style="padding-top:0px;">
        <td style="height:340px;width:100%;">
            <div id="grid"   >
            </div>
        </td>
    </tr>
    <tr>
        <td style="height:180px;">
            <p>
                已选择的关联单据(双击可以删除单据)
            </p><br />
            <select name="ltbSelectRelate" id="ltbSelectRelate" style="width: 100%; height:120px; font-size: small;" ondblclick="deleteForms(this);" size="4"/> 
        </td>
    </tr>
</table>
 <div class="ui_buttons cxl"  style="background:#d4e4f6;padding-right:20px;"> 
      <button type="button"  class="fj btn_c3 btn_s "  onclick="cancel();">取消</button>
            <button type="button" " id="subbtn"  class="fj btn_c3 btn_s ui_state_highlight" >确定</button>
    </div>
<script type="text/javascript">
    function cancel() {
        window.top.oDG.close();
    }
    function onQuery() {
        var grid = $("#grid").data("kendoGrid");
        grid.dataSource.read({ typeId: $("#slType").val(),ky:$("#txtkw").val() });
    }
    function deleteForms(tag) {
        $(tag).find("option:selected").remove(); 
    }
    function AddForm(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var ID = dataItem.ID;
        if ($("#ltbSelectRelate").find("option[value='" + ID + "']").length > 0) {
            return;
        }
        var str = '<option  value="' + dataItem.ID + '" ProcessGUID="' + dataItem.ProcessGUID + '" AppInstanceID="' + dataItem.AppInstanceID + '">' + dataItem.AppName + '</option>';
        $(str).appendTo("#ltbSelectRelate");
    }
    function loadData() {
        $("#grid").kendoGrid({
            dataSource: {
                serverPaging: true,
                pageSize: 15,
                transport: {
                    read: {
                        url: '@Url.Action("DialogFormsRelationSelect","Form")',
                        type: "POST",
                        dataType: "json"
                    },
                },
                schema: {
                    //取出資料陣列
                    data: function (d) { return d.Data; },
                    //取出資料總筆數(計算頁數用)
                    total: function (d) { return d.TotalCount; }
                }
            },
            height: 340,
            width:800,
             
                 pageable: {
                     refresh: true,
                     pageSizes: true
                 },
                 columns: [
                     { field: "ProcessGUID", title: "ProcessGUID", hidden: true },
                     { field: "AppInstanceID", title: "AppInstanceID", hidden: true },
                     { field: "ID", title: "审批序号", width:"80px"  },
                     { field: "AppName", title: "标题", },
                     { field: "ProcessName", title: "流程名称",  hidden: true },
                     { field: "CreatedByUserName", title: "拟稿人" ,width:"100px" },
                     {
                         field: "CreatedDateTime", width: "100px", title: "撰写时间", type: "date",
                         format: "{0:yyyy-MM-dd}"
                     },
                     {
                         command: { text: "添加", click: AddForm }, width: "140px", title: "操作"
                    } 

                 ]
             });
    }
    $(function () {
        loadData();
        $("#subbtn").click(function () {
            if ($("#ltbSelectRelate option").length == 0) return;
            var AppInstanceID=$("#hAppInstanceID").val();
            var ProcessGuid=$("#hProcessGuid").val();
            var FormTable = $("#hFormTable").val(); 
            var ActivityGuid = $("#hActivityGuid").val();
            var FormID = '@Request["FormID"]';
            if (AppInstanceID != null && AppInstanceID != "" && ActivityGuid !="") {
                var url = '@Url.Action("SaveProcessFormsRelation","Form")';
                var FormRelationArr = new Array(); 
                $.each($("#ltbSelectRelate option"), function (i, item) { 
                    var objFormRelation = new Object(); 
                    objFormRelation.AppInstanceFormTitle = $(item).html();
                    objFormRelation.RelationAppInstanceID = $(item).attr("AppInstanceID");
                    objFormRelation.RelationProcessGUID = $(item).attr("ProcessGUID");
                    objFormRelation.FormTable = FormTable; 
                    FormRelationArr.push(objFormRelation);
                });
                $.post(url, {
                    AppInstanceID: AppInstanceID, FormID: FormID,
                    FormsRelation: JSON.stringify(FormRelationArr)
                }, function (data) {
                    var arr = new Array();
                    $.each(data, function (i, item) {
                        var obj = new Object();
                        obj.ID = item.ID;
                        obj.AppInstanceID = item.RelationAppInstanceID;
                        obj.ProcessGUID = item.RelationProcessGUID;
                        obj.AppName = item.AppInstanceFormTitle;
                        arr.push(obj);
                    });
                    window.top.refresh(arr);
                    window.top.oDG.close();
                });

            } else {
                var arr = new Array();
                $.each($("#ltbSelectRelate option"), function (i, item) {
                    var obj = new Object();
                    obj.ID = $(item).attr("value");
                    obj.ProcessGUID = $(item).attr("ProcessGUID");
                    obj.AppInstanceID = $(item).attr("AppInstanceID");
                    obj.AppName = $(item).html();
                    arr.push(obj);
                }); 
                window.top.refresh(arr);
                window.top.oDG.close();
            }
             
            
        });
    });
    
</script>
